package com.dqy.twserver.server.handle.ehandle;

import com.dqy.twserver.server.dto.GameDto;
import com.dqy.twserver.server.support.Constans;
import com.dqy.twserver.server.support.MsgPush;
import com.dqy.twserver.server.support.SupportMap;
import com.dqy.twserver.server.support.data.RoomInfo;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GameEventSendMsgHandle implements GameEventHandle{
    static Logger logger = LoggerFactory.getLogger(GameEventSendMsgHandle.class);
    @Override
    public void handle(GameDto req, NioSocketChannel channel) throws Exception {
        logger.info("[GameEventSendMsgHandle] start");

        GameDto resp=new GameDto();

        String playerName = req.getPlayerName();
        String info = req.getInfo();
        String roomId = req.getRoomId();
        logger.info("[GameEventSendMsgHandle] playerName={},roomId={}",playerName,roomId);

        int rid= Integer.parseInt(roomId.substring(4,roomId.length()));

        RoomInfo roomInfo = SupportMap.roomMap.get(rid);
        if(roomInfo==null){
            logger.info("{}房间不存在",rid);
            resp.setSts(400);
            resp.setInfo("房间不存在");
            channel.writeAndFlush(resp);
        }

        String sendMsg=String.format("%s:%s",playerName,info);
        MsgPush.sendMsg(roomInfo.getPlayerList(),sendMsg,"info", Constans.EVENT_TYPE_SEND_ALL_MSG);

        resp.setSts(200);
        resp.setInfo("ok");
        channel.writeAndFlush(resp);

        logger.info("[GameEventSendMsgHandle] end");
    }
}
